package com.example.admindatasource.filepload;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class FileConstant {
    public static String STORE_PATH;
    public static String uploads = "uploads";
    public static String tmp = "tmp";
/*    *//**
     * 文件请求路径的URL前面部分
     *//*
    public static String baseUrl;*/


    /**
     * 文件下载展示的请求地址前缀
     */
    public static String requestLocationPrefix;
    /**
     * 设置全局文件类型检查
     */
    public static FileTypeCheckSwitch fileTypeCheckSwitch;

    public static List<String> whiteList;

    public static List<String> blackList;

    @Value("${file.filetype.whitelist}")
    public void setWhiteStrings(String whiteStrings) {
        FileConstant.whiteList = Arrays.stream(whiteStrings.split(",")).collect(Collectors.toList());
    }

    @Value("${file.filetype.blacklist}")
    public void setBlackStrings(String blackStrings) {
        FileConstant.blackList = Arrays.stream(blackStrings.split(",")).collect(Collectors.toList());
    }


    @Value("${file.request.location}")
    public void setRequestLocationPrefix(String requestLocationPrefix) {
        FileConstant.requestLocationPrefix = requestLocationPrefix;
    }

    @Value("${file.filetype.check}")
    public void setFileTypeCheckSwitch(Integer value) {
        FileTypeCheckSwitch[] values = FileTypeCheckSwitch.values();
        for (FileTypeCheckSwitch checkSwitch : values) {
            if (checkSwitch.value.equals(value)) {
                FileConstant.fileTypeCheckSwitch = checkSwitch;
            }
        }
    }

//    @Value("${file.request.baseUrl}")
//    public void setBaseUrl(String baseUrl) {
//        FileConstant.baseUrl = baseUrl;
//    }

    @Value("${file.store.path}")
    public void setStorePath(String storePath) {
        STORE_PATH = ObjectUtils.isEmpty(storePath) ? System.getProperty("catalina.home") : storePath;
    }
}
